import Mock from 'mockjs';

const Random = Mock.Random;

let Result = {
    code: 200,
    msg: '操作成功',
    data: null,
}

Mock.mock('/captcha', 'get', () => {
    Result.data = {
        token: Random.string(32),
        captchaImg: Random.dataImage('120x40', 'p7n5w')
    }
    return Result
})

Mock.mock('/login', 'post', () => {
    Result.data = {
        token: Random.string(32)
    }
    return Result
})

Mock.mock('/system/user_info', 'get', () => {
    Result.data = {
        id: '1',
        username: 'Admin',
        avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
    }
    return Result
})

Mock.mock('/logout', 'post', () => {
    return Result
})


Mock.mock(`/system/menus/list`, 'get', () => {
    Result.data = [
        {
            id: 1,
            name: '系统管理',
            perm_code: 'systemMange',
            icon: 'Setting',
            type: '0',
            path: '/',
            component: '',
            order: '0',
            state: '1',
            children: [
                {
                    id: 2,
                    name: '用户管理',
                    perm_code: 'rolesMange',
                    icon: 'User',
                    type: '1',
                    path: '/system/users',
                    component: 'system/User',
                    order: '0',
                    state: '1',
                    children: [],
                    created: 'time',
                    undated: 'time',
                },
                {
                    id: 3,
                    name: '角色管理',
                    perm_code: 'usersMange',
                    icon: 'Lollipop',
                    type: '1',
                    path: '/system/roles',
                    component: 'system/Roles',
                    order: '0',
                    state: '1',
                    children: [],
                    created: 'time',
                    undated: 'time',
                },
                {
                    id: 5,
                    name: '菜单管理',
                    perm_code: 'menusMange',
                    icon: 'Operation',
                    type: '1',
                    path: '/system/menus',
                    component: 'system/Menus',
                    order: '0',
                    state: '1',
                    children: [
                        {
                            id: 6,
                            name: '添加菜单',
                            perm_code: 'addMenus',
                            icon: 'add',
                            type: '2',
                            path: '',
                            component: '',
                            order: '',
                            state: '',
                            children: [],
                            created: 'time',
                            undated: 'time',
                        },
                    ],
                    created: 'time',
                    undated: 'time',
                },
            ],
            created: 'time',
            undated: 'time',

        },
        {
            id: 4,
            name: '系统工具',
            perm_code: 'systemTool',
            icon: 'Location',
            type: '0',
            path: '/',
            component: '',
            order: '0',
            state: '1',
            children: [],
            created: 'time',
            undated: 'time',
        },
    ]

    return Result
})

Mock.mock('/system/menus', 'post', () => {
    Result.data = [
        {
            title: '系统管理', name: 'systemMange', component: '', icon: 'Setting', path: '/', children:
                [
                    {
                        title: '用户管理',
                        name: 'usersMange',
                        component: 'system/User',
                        icon: 'User',
                        path: '/system/users',
                        children: [],
                        index: 'users'
                    },
                    {
                        title: '角色管理',
                        name: 'rolesMange',
                        component: 'system/Roles',
                        icon: 'Lollipop',
                        path: '/system/roles',
                        children: [],
                        index: 'roles'
                    },
                    {
                        title: '菜单管理',
                        name: 'menusMange',
                        component: 'system/Menus',
                        icon: 'Operation',
                        path: '/system/menus',
                        children: [],
                        index: 'menus'
                    },
                ]
            , index: '/'
        },
        {
            title: '系统工具',
            name: 'systemTool',
            component: '',
            icon: 'Location',
            path: '',
            children: [
                {
                    title: '字典管理',
                    name: 'dictTool',
                    component: '',
                    icon: 'Document',
                    path: 'dict',
                    children: [],
                    index: 'dict'
                },
            ],
            index: 'system_tool'
        },

    ]
    return Result
})

Mock.mock(`/system/menu/info`, 'post', (option) => {

    const str = option.body;
    const obj = JSON.parse(str);
    const index = obj.params.id;

    let data =
        [
            {
                id: 1,
                name: '系统管理',
                perm_code: 'systemMange',
                icon: 'Setting',
                type: 0,
                path: '/',
                component: '',
                order: '0',
                state: '1',
                children: [
                    {
                        id: 2,
                        name: '用户管理',
                        perm_code: 'rolesMange',
                        icon: 'User',
                        type: 1,
                        path: '/system/users',
                        component: 'system/User',
                        order: 0,
                        state: 1,
                        children: [],
                        created: 'time',
                        updated: 'time',
                    },
                    {
                        id: 3,
                        name: '角色管理',
                        perm_code: 'usersMange',
                        icon: 'Lollipop',
                        type: 1,
                        path: '/system/roles',
                        component: 'system/Roles',
                        order: 0,
                        state: 1,
                        children: [],
                        created: 'time',
                        updated: 'time',
                    },
                    {
                        id: 5,
                        name: '菜单管理',
                        perm_code: 'menusMange',
                        icon: 'Operation',
                        type: 1,
                        path: '/system/menus',
                        component: 'system/Menus',
                        order: 0,
                        state: 1,
                        children: [
                            {
                                id: 6,
                                name: '添加菜单',
                                perm_code: 'addMenus',
                                icon: 'add',
                                type: 2,
                                path: '',
                                component: '',
                                order: 0,
                                state: 1,
                                children: [],
                                created: 'time',
                                updated: 'time',
                            },
                        ],
                        created: 'time',
                        updated: 'time',
                    },
                ],
                created: 'time',
                updated: 'time',

            },
            {
                id: 4,
                name: '系统工具',
                perm_code: 'systemTool',
                icon: 'Location',
                type: 0,
                path: '/',
                component: '',
                order: 0,
                state: 1,
                children: [],
                created: 'time',
                updated: 'time',
            },
        ]

    // function findElem(index, data) {
    //     for (let e of data) {
    //         let parentId = e.id;
    //         if (e.children) {
    //             if (index === e.id) {
    //                 e.parentId = parentId;
    //                 return e;
    //             }
    //             const found = findElem(index, e.children);
    //             if (found) {
    //                 found.parentId = parentId;
    //                 return found ;
    //             }
    //         } else if (index === e.id) {
    //             e.parentId = parentId;
    //             return e;
    //         }
    //     }
    //     return null;
    // }

    function findElem(index, data, parentIds = []) {
        for (let e of data) {
            let parentId = e.id;
            if (e.children) {
                if (index === e.id) {
                    return {...e, parentIds};
                }
                const found = findElem(index, e.children, [...parentIds, parentId]);
                if (found) {
                    return found;
                }
            } else if (index === e.id) {
                return {...e, parentIds};
            }
        }
        return null;
    }

    function findParentId(elem) {
        if (elem.parentIds.length !== 0) {
            return elem.parentIds[elem.parentIds.length - 1];
        } else {
            return 0;
        }
    }

    const res = findElem(index, data);
    res.parentId = findParentId(res);
    Result.data = res;
    return Result;
})

Mock.mock(`/system/menu/delete`, 'post', () => {
    return Result.data = "删除成功"
})

Mock.mock('/system/authorities', 'post', () => {
    Result.data = ['hello'];
    return Result
})


/* 角色管理 */
Mock.mock('system/role/list', 'get', () => {
    Result.data = {
        'records': [
            {
                id: 0,
                created: 'time',
                'update': 'time',
                'state': 1,
                name: '超级管理员',
                code: 'admin',
                remark: 'Just do it.',
                menuIds: []
            },
            {
                id: 1,
                created: 'time',
                'update': 'time',
                'state': 1,
                name: '普通用户',
                code: 'normal',
                remark: 'Just do it.',
                menuIds: []
            }
        ],
        title: 2,
        size: 10,
        current: 1,
        orders: [],
        optimizeCountSql: true,
        hitCount: false,
        countId: null,
        maxLimit: null,
        searchCount: true,
        pages: 1
    }

    return Result
})

export default Mock
